package com.wss.common.base;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.bean.Event;
import com.wss.common.bean.HorizontalTabTitle;
import com.wss.common.utils.ValidUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.NotExistException;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

public abstract class BaseFragment extends Fragment {

    private Component emptyView;
    private HorizontalTabTitle tabTitle;
    private Unbinder unBinder;
    private Component rootView;

    public BaseFragment(Ability ability) {
        super(ability);
    }

    @Override
    public Component onCreateView(LayoutScatter instance) {
        rootView = instance.parse(ResourceTable.Layout_layout_base, null, false);
        ((ComponentContainer)rootView.findComponentById(ResourceTable.Id_fl_content))
                .addComponent(instance.parse(getLayoutId(), null, false));
        unBinder = ButterKnife.bind(this, rootView);
        if (registerEventBus()) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
        return rootView;
    }

    @Override
    public void onViewCreated(Component view) {
//        initView();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (unBinder != null) {
            unBinder.unbind();
        }
        if (registerEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    //***************************************空页面方法 start *************************************

    /**
     * 数据为空页面
     */
    protected void showEmptyView() {
        showEmptyView(context.getString(ResourceTable.String_no_data));
    }

    /**
     * 数据为空页面
     *
     * @param text 显示文案
     */
    protected void showEmptyView(String text) {
        showEmptyOrErrorView(text, ResourceTable.Media_bg_no_data, false);
    }

    /**
     * 请求数据报错页面
     */
    protected void showErrorView() {
        showErrorView(context.getString(ResourceTable.String_network_error_server_error));
    }

    /**
     * 请求数据报错页面
     *
     * @param text 显示文案
     */
    protected void showErrorView(String text) {
        showEmptyOrErrorView(text, ResourceTable.Media_bg_no_net, true);
    }

    /**
     * 请求数据报错或者为空页面
     *
     * @param text      提示文案
     * @param iconResId 显示的icon
     * @param showRefreshButton
     */
    public void showEmptyOrErrorView(String text, int iconResId, boolean showRefreshButton) {
        if (emptyView == null) {
            emptyView = rootView.findComponentById(ResourceTable.Id_vs_empty);
        }
        emptyView.setVisibility(Component.VISIBLE);
        ((Image)rootView
                .findComponentById(ResourceTable.Id_iv_empty))
                .setPixelMap(iconResId);
        ((Text)rootView.findComponentById(ResourceTable.Id_tv_empty)).setText(text);
        Component refreshButton = rootView.findComponentById(ResourceTable.Id_tv_try_again);
        if (showRefreshButton) {
            refreshButton.setVisibility(Component.VISIBLE);
            refreshButton.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    onRefreshRetry();
                }
            });
        } else {
            refreshButton.setVisibility(Component.HIDE);
        }
    }

    /**
     * 隐藏空页面
     */
    protected void hideEmptyView() {
        if (emptyView != null) {
            emptyView.setVisibility(Component.HIDE);
        }
    }

    /**
     * 刷新重试
     */
    protected void onRefreshRetry() {
    }

    //***************************************空页面方法 end*********************************


    /**
     * 停止刷新和加载更多
     *
     * @param layout 刷新layout
     */
    protected void stopRefresh(BgarefreshLayout layout) {
        if (layout == null) {
            return;
        }
        layout.endLoadingMore();
        layout.endRefreshing();
    }

    /**
     * 返回Fragment的根布局
     *
     * @return View
     */
    public Component getRootView() {
        return rootView;
    }

    /**
     * 给Fragment设置TabTile数据
     *
     * @param data tabtitle
     */
    public void setTabTitle(HorizontalTabTitle data) {
        this.tabTitle = data;
    }

    /**
     * 返回ViewPager+Fragment滑动 设置给子Fragment的Tab数据
     *
     * @return HorizontalTabTitle
     */
    public HorizontalTabTitle getTabTitle() {
        return ValidUtils.isValid(tabTitle) ? tabTitle : new HorizontalTabTitle("");
    }

    /**
     * 子类接受事件 重写该方法
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(Event event) {
    }

    /**
     * 需要接收事件 重新该方法 并返回true
     */
    protected boolean registerEventBus() {
        return false;
    }

    /**
     * 返回页面layout
     *
     * @return layout
     */
    protected abstract int getLayoutId();

    /**
     * 初始化View
     */
    protected abstract void initView();
}
